<?php

namespace App\Admin\Controllers;

use App\Admin\Grid\NGrid;
use App\Admin\Tools\StuffWages\StuffWagesCreateTool;
use App\Admin\Tools\StuffWages\StuffWagesDeleteTool;
use App\Admin\Tools\StuffWages\StuffWagesEditTool;
use App\Models\AdminUsersRelatedModel;
use App\Models\StuffWagesModel;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Grid\Displayers\Actions;
use Encore\Admin\Grid\Tools;
use Illuminate\Support\Facades\Auth;

class StuffWagesController extends AdminController
{
    protected $title = '员工工资';

    public function grid(): NGrid
    {
        $grid = new NGrid(new StuffWagesModel);
        $grid->model()->leftJoin('admin_users as user', 'user.id', '=', 'stuff_wages.user_id')
            ->leftJoin('admin_users as operate', 'operate.id', '=', 'stuff_wages.operate_id')
            ->leftJoin('admin_users as audit', 'audit.id', '=', 'stuff_wages.audit_id')
            ->orderByDesc('stuff_wages.id')
            ->select([
                'stuff_wages.*',
                'user.name',
                'operate.name as operate_name',
                'audit.name as audit_name',
            ]);

        $grid->filter(function ($filter) {
            $filter->disableIdFilter();
            $filter->column(1 / 2, function ($filter) {
                $filter->equal('user_id', '员工姓名')->select((new AdminUsersRelatedModel)->stuff());
                $filter->between('data', __('finance_income.incomeDate'))->date();
            });
        });

//        if (!ISADMIN) {
//            if (ISMAIN) $grid->model()->where('stuff_wages.main_id', USERID);
//            else $grid->model()->where('stuff_wages.main_id', MAINID);
//        } else $grid->model()->where('stuff_wages.operate_id', USERID);
        if (!ISADMIN) {
            if (in_array(Auth::user()->roles->first()->slug, ['boss', 'finance'])) $grid->model()->where('stuff_wages.main_id', MAINID);
            else $grid->model()->where('stuff_wages.operate_id', USERID);
        }
        if(Auth::user()->roles->first()->slug == 'Administrator') $grid->column('id', 'ID')->sortable();
        $grid->column('date', '日期')->display(function () {
            return date('Y-m-d', strtotime($this->date));
        });
        $grid->column('name', '员工名称');
        $grid->column('basic_wages', '基本薪资');
        $grid->column('commission', '提成');
        $grid->column('deduct', '应扣费用');
        $grid->column('actual_wages', '实发工资');
        $grid->column('remark', '备注');
        $grid->column('operate_name', '操作人');
        $grid->column('created_at', '创建时间')->display(function () {
            return date('Y-m-d', strtotime($this->date));
        });
        $grid->column('status', __('l.status'))->display(function () {
            return $this->status === 0 ? '待审核' : __('l.auditStatusArr')[$this->status];
        });
        $grid->column('audit_name', '审核人');
        $grid->column('audit_remark', '审核意见');
        $grid->actions(function (Actions $action) {
            $action->disableDelete();
            $action->disableEdit();
            $action->disableView();
            if ((int)$action->row->status !== 1) $action->append(new StuffWagesEditTool($action->row->id));
//            if ((int)$action->row->status !== 1) $action->append(new StuffWagesDeleteTool($action->row->id));
        });
        $grid->tools(function (Tools $tools) {
            $tools->append(new StuffWagesCreateTool());
        });
        $grid->disableActions(false);
        $grid->disableCreateButton();
        return $grid;
    }
}
